package com.photobook.base.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collection;
import java.util.List;
import java.util.Vector;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import android.content.Context;
import android.util.Log;

import com.photobook.R;

/**
 * The Class Utils.
 * 
 * @author hildon.lima
 */
public class Util {

	/**
	 * Save array.
	 * 
	 * @param filename
	 *            the filename
	 * @param output_field
	 *            the output_field
	 */
	public static void saveArray(String filename, List<String> output_field) {
		try {
			FileOutputStream fos = new FileOutputStream(filename);
			GZIPOutputStream gzos = new GZIPOutputStream(fos);
			ObjectOutputStream out = new ObjectOutputStream(gzos);
			out.writeObject(output_field);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.getStackTrace();
		}
	}

	/**
	 * Load array.
	 * 
	 * @param filename
	 *            the filename
	 * @return the list
	 */
	@SuppressWarnings("unchecked")
	public static List<String> loadArray(String filename) {
		try {
			FileInputStream fis = new FileInputStream(filename);
			GZIPInputStream gzis = new GZIPInputStream(fis);
			ObjectInputStream in = new ObjectInputStream(gzis);
			List<String> read_field = (List<String>) in.readObject();
			in.close();
			return read_field;
		} catch (Exception e) {
			e.getStackTrace();
		}
		return null;
	}

	/**
	 * List files as array.
	 * 
	 * @param directory
	 *            the directory
	 * @param filter
	 *            the filter
	 * @param recurse
	 *            the recurse
	 * @return the file[]
	 */
	public static File[] listFilesAsArray(File directory,
			FilenameFilter[] filter, int recurse) {
		Collection<File> files = listFiles(directory, filter, recurse);

		File[] arr = new File[files.size()];
		return files.toArray(arr);
	}

	/**
	 * List files.
	 * 
	 * @param directory
	 *            the directory
	 * @param filter
	 *            the filter
	 * @param recurse
	 *            the recurse
	 * @return the collection
	 */
	public static Collection<File> listFiles(File directory,
			FilenameFilter[] filter, int recurse) {

		Vector<File> files = new Vector<File>();

		File[] entries = directory.listFiles();

		if (entries != null) {
			for (File entry : entries) {
				for (FilenameFilter filefilter : filter) {
					if (filter == null
							|| filefilter.accept(directory, entry.getName())) {
						files.add(entry);
						Log.v("ImageViewFlipper", "Added: " + entry.getName());
					}
				}
				if ((recurse <= -1) || (recurse > 0 && entry.isDirectory())) {
					recurse--;
					files.addAll(listFiles(entry, filter, recurse));
					recurse++;
				}
			}
		}
		return files;
	}

	/**
	 * Checks if is large screen
	 * 
	 * @param context
	 * @return {@link Boolean}
	 */
	public static boolean isLargeScreen(Context context) {
		return context.getResources().getBoolean(R.bool.isLandscape);
	}
}